<?php
/**
 * SummerCamps Model for SummerCamps World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 
/**
 * SummerCamps Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
//Naming convention for a model:
// [Component Name]["Model"][Model Name]
//-[SummerCamps][Model][SummerCamps]
//The Controller will automatically load a model into the view with the same name. 
class SummerCampsModelSingle extends JModel
{
	/**
	* Camps data array
	*
	* @var array
	*/
	var $_data;
	
	
	function __construct()
	{
		parent::__construct();
	}
 
    /**
     * Returns the query
     * return the list of camps from the database
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildQuery()
    {
        $query = ' SELECT * '
            . ' FROM #__summercamps WHERE Approved=1';
        return $query;
    }
 
    /**
     * METHODS THAT START WITH 'get' CAN BE ACCESSED USING THE get() METHOD
     * OF THE JView CLASS. 
     * 
     * EX: $items =& &this->get('Data');
     * 
     * Retrieves the camp data
     * @return array Array of objects containing the data from the database
     */
    function getData()
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList( $query );
        }
 
        return $this->_data;
    }
    
    /**
     * Returns the query
     * return the list of camps from the database
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildIDQuery($id)
    {
        $query = ' SELECT * '
            . ' FROM #__summercamps WHERE ID='.$id.' AND Approved=1';
        return $query;
    }
    /**
     * METHODS THAT START WITH 'get' CAN BE ACCESSED USING THE get() METHOD
     * OF THE JView CLASS. 
     * 
     * EX: $items =& &this->get('Data');
     * 
     * Retrieves the camp data
     * @return array Array of objects containing the data from the database
     */
    function getDataFromID($id)
    {
        //Let's load the data if it doesn't already exist
        if (empty( $this->_iddata ))
        {
            $query = $this->_buildIDQuery($id);
            $this->_iddata = $this->_getList( $query );
        }
 
        return $this->_iddata;
    }
    
	/**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{	
		$row =& $this->getTable('camp');

		$data = JRequest::get( 'post' );

		// Bind the form fields to the camp table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the camp record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}

		return true;
	}
}